package com.fjh.controller.sys;

import com.fjh.common.Result;
import com.fjh.domain.SysPermission;
import com.fjh.dto.PermissionDto;
import com.fjh.realm.ActiveUser;
import com.fjh.service.PermissionService;
import com.fjh.vo.DTreeVo;
import com.fjh.vo.SelectNode;
import com.fjh.vo.TreeNode;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/sys/perms")
public class SysPermissionController {
    @Autowired
    private PermissionService permissionService;

    /**
     * 查询用户的菜单数据
     * @return
     */
    @GetMapping("/tree.do")
   public List<TreeNode> getTree(){
//       List<SysPermission> sysPermissions = permissionService.selectAllSysPermission();
//       List<TreeNode> treeNodes = permissionService.convertTreeNode(sysPermissions);
        Subject subject = SecurityUtils.getSubject();
        ActiveUser  principal = (ActiveUser) subject.getPrincipal();
        return principal.getMenus();
   }

   @GetMapping("/select.do")
   public List<SelectNode> getSelect(){
        List<SysPermission> sysPermissions = permissionService.selectAllSysPermission();
        List<SelectNode> selectNodes = permissionService.convertSelectNode(sysPermissions);
        return selectNodes;
   }

    /**
     * 分页查询我们的权限数据
     *
     * @param dto
     * @return
     */
   @RequestMapping("/findByPage.do")
   public Result findByPage(PermissionDto dto){
        List<SysPermission>sysPermissions = permissionService.findByPage(dto);
        return Result.ok(sysPermissions);
   }

    /**
     * 新增一个权限
     * @param dto
     * @return
     */
   @PostMapping("add.do")
   public Result addPerms(@RequestBody PermissionDto dto){
       Boolean isOk = permissionService.addPerms(dto);
       return isOk?Result.ok() : Result.fail();
   }

    /**
     * 删除一个权限
     * @param ids
     * @return
     */
   @PostMapping("/delete.do/{ids}")
   public Result deletePerms(@PathVariable("ids") Integer[] ids){
        Boolean isOk = permissionService.batchRemove(ids);
        return isOk ? Result.ok() : Result.fail();
   }

    /**
     * 修改一个权限的数据
     * @param dto
     * @return
     */
   @PostMapping("/update.do")
   public Result updatePerms(@RequestBody PermissionDto dto){
        Boolean isOk = permissionService.updatePerms(dto);
        return isOk? Result.ok():Result.fail();
   }

    /**
     * 查询角色的权限
     * @param roleId
     * @return
     */
   @RequestMapping("/getRolePerms.do")
   public Result getPermsByRoleId(Integer roleId){
       //1.得到当前角色的权限
       List<Integer> permsIds = permissionService.getPermsByRoleId(roleId);
       //2.我们需要得到所有的权限
       List<SysPermission> sysPermissions = permissionService.selectAllSysPermission();
       List<DTreeVo> dtreeVos = convertAndMapping(sysPermissions,permsIds);
       return Result.ok(dtreeVos);
   }

    /**
     * 给角色分配权限
     * @param roleId 角色的Id
     * @param permsIds 权限的集合
     * @return
     */
   @RequestMapping("/assign.do")
   public Result assign(Integer roleId,@RequestBody Integer[] permsIds){
       Boolean isOk = permissionService.assign(roleId,permsIds);
       return isOk? Result.ok():Result.fail();
   }

    /**
     * 得到一个DTree格式的树
     * @param sysPermissions 权限数据
     * @param permsIds 要选择的权限
     * @return
     */
    private List<DTreeVo> convertAndMapping(List<SysPermission> sysPermissions, List<Integer> permsIds) {
        List<DTreeVo> dTreeVos = null;
        if (sysPermissions != null && sysPermissions.size() >0){
            dTreeVos = new ArrayList<>();
            for (SysPermission sysPermission:sysPermissions) {
                DTreeVo dTreeVo = new DTreeVo();
                BeanUtils.copyProperties(sysPermission,dTreeVo);
                // 选择和不选择
                Map<String, Object> checked = new HashMap<>();
                if (permsIds.contains(dTreeVo.getId())){
                    checked.put("type",0);
                    checked.put("checked",1);
                }else {
                    checked.put("type", 0);
                    checked.put("checked", 0);
                }
                dTreeVo.setCheckArr(checked);
                dTreeVos.add(dTreeVo);
            }
            return dTreeVos;
        }
        return Collections.emptyList();
    }
}
